package com.java110.mall.acct.payment.business;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.account.AccountBondObjDetailDto;
import com.java110.dto.account.AccountBondObjDto;
import com.java110.dto.payment.PaymentOrderDto;
import com.java110.dto.shop.ShopDto;
import com.java110.intf.acct.IAccountBondObjDetailInnerServiceSMO;
import com.java110.intf.acct.IAccountBondObjInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.mall.acct.payment.IPaymentBusiness;
import com.java110.po.account.AccountBondObjDetailPo;
import com.java110.po.account.AccountBondObjPo;
import com.java110.po.shop.ShopPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 店铺缴保证金
 */
@Service("bondPay")
public class BondPayBusiness implements IPaymentBusiness {

    @Autowired
    private IAccountBondObjInnerServiceSMO accountBondObjInnerServiceSMOImpl;

    @Autowired
    private IAccountBondObjDetailInnerServiceSMO accountBondObjDetailInnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Override
    public PaymentOrderDto unified(ICmdDataFlowContext context, JSONObject reqJson) {
        //计算服务费
        ShopDto shopDto = new ShopDto();
        shopDto.setShopId(reqJson.getString("shopId"));
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);

        Assert.listOnlyOne(shopDtos, "商铺不存在");
        shopDto = shopDtos.get(0);

        AccountBondObjDto accountBondObjDto = new AccountBondObjDto();
        accountBondObjDto.setObjId(shopDto.getShopId());
        accountBondObjDto.setState(AccountBondObjDto.STATE_BUY_W);
        List<AccountBondObjDto> accountBondObjDtos = accountBondObjInnerServiceSMOImpl.queryAccountBondObjs(accountBondObjDto);
        if (ListUtil.isNull(accountBondObjDtos)) {
            throw new CmdException("未查询到店铺保证金");
        }
        String orderId = accountBondObjDtos.get(0).getBobjId();
        String feeName = shopDtos.get(0).getShopName() + "保证金";
        double money = Double.parseDouble(accountBondObjDtos.get(0).getReceivableAmount());
        PaymentOrderDto paymentOrderDto = new PaymentOrderDto();
        paymentOrderDto.setOrderId(orderId);
        paymentOrderDto.setMoney(money);
        paymentOrderDto.setName(feeName);
        paymentOrderDto.setProfitSharing(false);
        return paymentOrderDto;
    }

    @Override
    public void notifyPayment(PaymentOrderDto paymentOrderDto, JSONObject reqJson) {
        AccountBondObjDto accountBondObjDto = new AccountBondObjDto();
        accountBondObjDto.setBobjId(paymentOrderDto.getOrderId());
        List<AccountBondObjDto> accountBondObjDtos = accountBondObjInnerServiceSMOImpl.queryAccountBondObjs(accountBondObjDto);
        if (ListUtil.isNull(accountBondObjDtos)) {
            throw new CmdException("未查询到店铺保证金");
        }
        AccountBondObjPo accountBondObjPo = new AccountBondObjPo();
        accountBondObjPo.setBobjId(accountBondObjDtos.get(0).getBobjId());
        accountBondObjPo.setReceivedAmount(accountBondObjDtos.get(0).getReceivableAmount());
        accountBondObjPo.setState(AccountBondObjDto.STATE_BUY_FINISH);
        accountBondObjPo.setRemark("已通过微信支付，完成支付");
        accountBondObjInnerServiceSMOImpl.updateAccountBondObj(accountBondObjPo);


        AccountBondObjDetailPo accountBondObjDetailPo = new AccountBondObjDetailPo();
        accountBondObjDetailPo.setBobjId(accountBondObjDtos.get(0).getBobjId());
        accountBondObjDetailPo.setObjId(accountBondObjDtos.get(0).getObjId());
        accountBondObjDetailPo.setBondType(accountBondObjDtos.get(0).getBondType());
        accountBondObjDetailPo.setDetailId(GenerateCodeFactory.getGeneratorId("11"));
        accountBondObjDetailPo.setReceivableAmount(accountBondObjDtos.get(0).getReceivableAmount());
        accountBondObjDetailPo.setRemark("已通过微信支付，完成支付");
        accountBondObjDetailPo.setStartTime(accountBondObjDtos.get(0).getStartTime());
        accountBondObjDetailPo.setReceivedAmount(accountBondObjDtos.get(0).getReceivableAmount());
        accountBondObjDetailPo.setState("3308");
        accountBondObjDetailPo.setEndTime(accountBondObjDtos.get(0).getEndTime());
        accountBondObjDetailPo.setObjName(accountBondObjDtos.get(0).getObjName());
        accountBondObjDetailInnerServiceSMOImpl.saveAccountBondObjDetail(accountBondObjDetailPo);

        ShopPo shopPo = new ShopPo();
        shopPo.setShopId(accountBondObjDtos.get(0).getObjId());
        shopPo.setState(ShopDto.STATE_F);
        shopInnerServiceSMOImpl.updateShop(shopPo);
    }
}
